package com.tao.utilslib.file;

import android.content.Context;

import com.tao.utilslib.log.LogUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

public class AssetsUtils {
    public static boolean copyAssets2Path(Context context, String assetsPath, String filePath) {
        try {
            InputStream open = context.getAssets().open(assetsPath);
            return FileUtil.copyStream2File(open, filePath);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String[] listPath(Context context, String path) {
        try {
            return context.getAssets().list(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String[0];
    }

    public static void copyAssetsFilder(Context context, String path, String toFolder) {
        String[] listPath = listPath(context, path);

        try {
            LogUtil.e(path,"   ",toFolder," copyAssetsFilder:", listPath ==null ?"": Arrays.toString(listPath));
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (listPath == null || listPath.length == 0) {
            copyAssets2Path(context ,path,toFolder);
        }else {
            for (String p : listPath) {
                LogUtil.e(" path:" ,p );
                copyAssetsFilder(context,path+"/"+p ,toFolder+"/"+p);
            }
        }
    }
}
